<template>
  <div class="about">
    <div class="top">
      特色课
      <van-icon class="search" name="search" />
    </div>
    <van-dropdown-menu>
      <van-dropdown-item title="分类" ref="item">
        <div class="itemBtn">
          <button>重置</button>
          <button class="que">确认</button>
        </div>
      </van-dropdown-item>
      <van-dropdown-item title="排序" ref="item1">
        <ul class="sort">
          <li v-for="(item,index) in optionList" :key="index" :class="{active1:liIndex1==index}"
            @click="check1(item.value,index)"> {{item.text}}</li>
        </ul>
      </van-dropdown-item>
      <van-dropdown-item title="筛选" ref="item2">
        <div>
          <ul class="btn">
            <li :class="{ active1: liIndex2 == index }" v-for="(item, index) in arr" :key="index"
              @click="check2(item.id,index)">
              {{ item.name}}
            </li>
          </ul>
        </div>
      </van-dropdown-item>
    </van-dropdown-menu>
    <div>
      <course-list v-for="(item,index) in list" :key="index" :itemObj="item"></course-list>
      <h1 v-show="last">没有数据了</h1>
    </div>
    <tabbar></tabbar>
  </div>
</template>
<script>
import { getCourse, getClassify,getClassifyDetail} from "@/http/request"
import tabbar from "@/components/tabbar.vue"
import courseList from "@/components/courseList.vue"
export default {
  data() {
    return {
      arr: [],
      liIndex1: 0,
      liIndex2: 0,
      switch1: false,
      switch2: false,
      optionList: [{ text: "综合排序", value: 0 },
      { text: "最新", value: 1 },
      { text: "最热", value: 2 },
      { text: "价格从低到高", value: 3 },
      { text: "价格从高到低", value: 4 },
      ],
      courseList: {
        course_type: 0,
        limit: 5,
        page: 1,
        order_by: 0
      },
      list: [],
      last_page: 1,
      last: false,
      courseDetail:[]
    }
  },
  mounted() {
    this.getCourseList()
    this.getClassifyList()

    window.onscroll = () => {
      //变量scrollTop是滚动条滚动时，距离顶部的距离
      let scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
      //变量windowHeight是可视区的高度
      let windowHeight = document.documentElement.clientHeight || document.body.clientHeight;
      //变量scrollHeight是滚动条的总高度
      let scrollHeight = document.documentElement.scrollHeight || document.body.scrollHeight;
      //滚动条到底部的条件
      //scrollTop!=0是由于其他功能在body上设置了最小高度100vh，切换tab的时候body高度位100vh。
      if ((Math.ceil(scrollTop + windowHeight) == parseInt(scrollHeight)) && scrollTop != 0) {
        if (this.courseList.page < this.last_page) {
          this.courseList.page += 1;
          this.getCourseList();
          this.last = false;
          console.log("请求数据--------")
        } else {
          this.last = true;
        }
      }
    }
    window.addEventListener("beforeunload",(e)=>{window.scroll(0,0)})
  },
  created() {

  },
  methods: {
    async getClassifyList() {
      let res = await getClassify()
      // console.log(res);
      this.arr = res.data.data.appCourseType
    },
    async getCourseList() {
      let res = await getCourse(this.courseList)
      // console.log(res.data.data);
      this.last_page = res.data.data.last_page
      this.list = [...this.list, ...res.data.data.list]
    },
    check1(v, i) {
      this.liIndex1 = i
      this.courseList.page = 1
      this.list = []
      this.courseList.order_by = v;
      this.getCourseList()
      this.$refs.item1.toggle();
    },
    check2(v, i) {
      this.liIndex2 = i
      this.courseList.page = 1
      this.list = []
      this.courseList.course_type = v
      this.getCourseList()
      this.$refs.item2.toggle();
    },
  },
  components: {
    tabbar,
    courseList,
  }
}
</script>
<style lang="scss" scoped>
.about {
  background-color: #f2f3f5;
  .active1 {
    color: #ee0a24;
  }

  .top {
    height: 54px;
    width: 100%;
    line-height: 54px;
    background-color: #fff;
    font-size: 20px;
    text-align: center;

    .search {
      float: right;
      margin-right: 10px;
      margin-top: 15px;
    }
  }

  .itemBtn {
    display: flex;
    height: 120px;
    justify-content: space-around;

    button {
      width: 45%;
      height: 45px;
      margin-top: 40px;
      background-color: #fff;
      border-radius: 5px;
      border: 2px solid #1989fa;
      outline: none;
    }
  }

  .sort {
    li {
      height: 30px;
      line-height: 30px;
      text-align: center;
      border-bottom: 1px solid #eee;
    }
  }

  .btn {
    display: flex;
    justify-content: space-between;
    flex-wrap: wrap;

    li {
      flex: 20%;
      height: 30px;
      line-height: 30px;
      text-align: center;
      background-color: #f5f5f5;
      margin: 5px 20px;
    }
  }
}
</style>